This unique method, simply takes an array and it returns that array with all
duplicates removed. That means if I have the same number a few times or the
same string, it'll remove any duplicates. Let's run this.
Back inside Atom, we can add this utility into our project, we'll comment out our
_.isString
calls and we will make a variable called
filteredArray
. This will be the
array without the duplicates, and what we'll do is call, after the equal sign,
_.uniq
.
Now, as we know, this takes an array. And since we're trying to use the unique
function, we'll pass in an array with some duplicates. Use your name twice as a
string; I'll use my name once, followed by the number
1
, followed by my name
again. Then I can use
1
,
2
,
3
, and
4
as shown here:
console.log('Starting app.js');
const fs = require('fs'); const os
= require('os'); const _ =
require('lodash');
const notes = require('./notes.js');
// console.log(_.isString(true));
// console.log(_.isString('Gary'));
var filteredArray = _.uniq(['Gary', 1, 'Gary', 1, 2, 3, 4]); console.log();
// console.log('Result:', notes.add(9, -2));
// var user = os.userInfo();
//
// fs.appendFile('greetings.txt', `Hello ${user.username}! You are ${notes.age}.`);
Now, if things go as planned, we should get an array with all the duplicates
removed, which means we'll have one instance of
Gary
, one instance of
1
, and
then
2
,
3
, and
4
, which don't have duplicates.
The last thing to do is to print that using
console.log
so we can view it inside the
Terminal. I'll pass in this
filteredArray
variable to our
console.log
statement as
shown in the following code:
console.log('Starting app.js');
const fs = require('fs'); const os
= require('os'); const _ =
require('lodash');
const notes = require('./notes.js');
// console.log(_.isString(true));
// console.log(_.isString('Gary'));
var filteredArray = _.uniq(['Gary', 1, 'Gary', 1, 2, 3, 4]); console.log(filteredArray);